package com.feituan.manager.controller;


import com.alibaba.fastjson.JSON;
import com.feituan.core.persistence.mapper.MerchantMapper;
import com.feituan.core.persistence.service.*;
import com.feituan.core.pojo.entity.*;
import com.feituan.core.pojo.request.CateReq;
import com.feituan.core.pojo.request.CommonPageReq;
import com.feituan.core.pojo.request.CouponAndCount;
import com.feituan.core.pojo.request.MerchantSetProductReq;
import com.feituan.core.pojo.request.UserCouponPageReq;
import com.feituan.core.pojo.request.UserCouponSendReq;
import com.feituan.core.pojo.request.UserReq;
import com.feituan.core.pojo.response.CateRes;
import com.feituan.core.pojo.response.MerchantProductRes;
import com.feituan.core.pojo.response.Response;
import com.feituan.core.pojo.response.UserRes;
import com.feituan.core.pojo.response.page.PageData;
import com.feituan.core.pojo.vo.CouponVo;
import com.feituan.core.pojo.vo.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaohu
 * @since 2024-05-09
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
public class UserController extends BaseController{
    @Autowired
    private IUserService iUserService;

    @Autowired
    private IUserCouponService iUserCouponService;
    @Autowired
    private ICouponService iCouponService;

    @PostMapping("/list_page")
    @ApiOperation("")
    public Response<PageData<UserRes>> listPage(@Valid UserReq request) {
        setDefaultPageParameters(request);
        return Response.success(iUserService.queryPage(request));
    }



    @PostMapping("/get_user_coupon")
    @ApiOperation("发放优惠券查询")
    public Response<PageData<CouponVo>> getUserCoupon(@Valid UserCouponPageReq request) {
        setDefaultPageParameters(request);
        PageData<UserCoupon> data = iUserCouponService.queryPage(request);
        PageData<CouponVo> pageData=new PageData<>();
        pageData.setCurrent(data.getCurrent());
        pageData.setTotal(data.getTotal());
        if(data.getRecords()!= null ){
            List<CouponVo> couponVos=new ArrayList<>();
            if(data.getRecords().size()>0){
                Map<Long,Coupon> map=iCouponService.lambdaQuery().in(Coupon::getId,data.getRecords().stream().map(UserCoupon::getCouponId).distinct().collect(Collectors.toList()))
                        .list().stream().collect(Collectors.toMap(Coupon::getId,e->e));
                for(UserCoupon userCoupon: data.getRecords()){
                    CouponVo couponVo=new CouponVo();
                    Coupon coupon=map.get(userCoupon.getCouponId());
                    BeanUtils.copyProperties(coupon,couponVo);
                    couponVo.setUserId(userCoupon.getUserId());
                    couponVos.add(couponVo);
                }
            }
            pageData.setRecords(couponVos);
        }
        return Response.success(pageData);
    }

    /**
     * 修改用户积分
     * @param user user
     * @return
     */
    @PostMapping("/updateIntegration")
    public Response<Boolean> updateIntegration(User user) {
        return Response.success(this.iUserService.updateById(user));
    }

}
